package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.JobCommentConstant;
import com.antler.smt.entity.JobOrderCommentEntity;
import com.antler.smt.entity.RecruitmentInfoEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.JobOrderCommentEntityMapper;
import com.antler.smt.portal.mapper.JobOrderCommentInfoMapper;
import com.antler.smt.portal.mapper.RecruitmentInfoEntityMapper;
import com.antler.smt.request.portal.JobOrderCommentReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.JobCommentService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.utils.ReturnResultUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单评价实现
 *
 * @author zhangyi
 * @date 2018/7/13 13:16
 */
@Service
public class JobCommentServiceImpl implements JobCommentService {


    /**
     * 添加日志
     */
    private Logger log = LogUtil.get();

    @Autowired
    private JobOrderCommentEntityMapper jobOrderCommentEntityMapper;

    @Autowired
    private JobOrderCommentInfoMapper jobOrderCommentInfoMapper;

    @Autowired
    private RecruitmentInfoEntityMapper recruitmentInfoEntityMapper;

    /**
     * 插入订单信息
     *
     * @param jobOrderCommentReq
     * @return
     */
    @Override
    public Result insertComment(JobOrderCommentReq jobOrderCommentReq) {
        //创建评价实体
        JobOrderCommentEntity jobOrderCommentEntity = new JobOrderCommentEntity();
        jobOrderCommentEntity.setCommentLevel(jobOrderCommentReq.getCommentLevel());
        jobOrderCommentEntity.setCommentPeople(jobOrderCommentReq.getCommentPeople());
        jobOrderCommentEntity.setCommentPlatform(jobOrderCommentReq.getCommentPlatform());

        //更新时间
        jobOrderCommentEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
        jobOrderCommentEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        int i = jobOrderCommentEntityMapper.insertSelective(jobOrderCommentEntity);
        return ReturnResultUtil.getResult("评论插入成功！","评论插入失败！",i);
    }

    /**
     * 获取订单信息，展示不同的信息
     *
     * @param recruitmentId
     * @param memberId
     * @return
     */
    @Override
    public Result selectComment(Long recruitmentId,Long memberId) {
        //获取订单实体
        JobOrderCommentEntity jobOrderCommentEntity =
                jobOrderCommentInfoMapper.selectJobOrderComment(recruitmentId);
        //获取招聘需求实体
        RecruitmentInfoEntity recruitmentInfoEntity =
                recruitmentInfoEntityMapper.selectByPrimaryKey(recruitmentId);

        if(recruitmentInfoEntity.getMemberId().equals(memberId)){
            //判别是否是本人
            return new Result(jobOrderCommentEntity);
        }else{
            if(jobOrderCommentEntity.getShowHomepage().equals(JobCommentConstant.HOME_YES)){
                //判别是否首页展示
                return new Result(jobOrderCommentEntity);
            }else{
                if (jobOrderCommentEntity.getAuditStatus().equals(JobCommentConstant.AUDIT_SUCCESS)){
                    //判别订单是否有效
                    return new Result(jobOrderCommentEntity);
                }else {
                    //订单无效，不显示
                    return new Result(ResultCodeEnum.APPEND_DATA_ERROR,"订单无效！");
                }
            }
        }
    }
}
